
/**
 * 
 * @param {
 * for item in dbo.get_rules_all:
        if item['is_use']==0 and check_rule(item['conditions'],cond)==1 :
            if item['is_temp']==0 :
                result=item['result']
            else:
                item['is_use']=1
                cond.append(item['result'])
            print_tmp(item)
        } conds 
 */
function identify(rules_data,conds){
    var result='不认识'
    for (var i in rules_data) {
        var item=rules_data[i]
        if (item['is_use']==0 && check_rule(item['conditions'],conds)==1) {
            if(item['is_temp']==0){
                result=item['result']
            }else{
                item['is_use']==1
                conds.push(item['ft_id'])
            }
            print_tmp(item)   
        }
    }
    
    return result
}

/**
 * def check_rule(rule_cond,cond):
    rc_len=len(rule_cond)
    cd_len=len(cond)
    for rc_item in rule_cond:
        j=0
        for cd_item in cond:
            print("cd_item=={}".format(rc_item['id']),end=",")
            print("cd_item=={}".format(cd_item))
            if rc_item['id'] == cd_item:
                break
            j+=1
        if j>=cd_len:
            return 0
    return 1
 */
/**
 * 
 * @param {库中的规则} rule_cond 
 * @param {查询条件的规则} conds 
 */
function check_rule(rule_cond,conds){
    for (var i in rule_cond){
        rc_item=rule_cond[i]
        var j=0
        for(;j<conds.length;j++){
            //console.info("----rc_item----- "+rc_item['cond_id']+" and ----conds[i]-----"+conds[j])
            //
            if(rc_item.cond_id==conds[j]){
                break
            }
        }

        if(j>=conds.length){
            return 0
        }
    }
    return 1
}

function print_tmp(rules){
    var str=""
    console.log(rules['conditions'])
    for (let index = 0; index < rules['conditions'].length; index++) {
        
        if(index==0){
            str=rules['conditions'][index]['cond_name']
        }else{
            str+=' + '+rules['conditions'][index]['cond_name']
        }
        
    }
    str=str+' ---->'+rules['result']
    $("#induction_detail").append("<p>"+str+"</p")
    return str
}
    

/**
 * 正向推理
 */
function forward_induction(){
    
    $.ajax({
        url:'/show-rules',   
        method:'get',
        async:false,   
        dataType:'JSON',    
        success:function(res){ 
          
          layui.form.on('submit(btnSubmit)', function (data) {
            $("#induction_detail").html("<p>开始推理过程。。。。</p")
            //表单数据formData
            var formData = data.field;
            var conds=[]
            for (const key in formData) {
                if(key.indexOf('conditions_select')!=-1){
                    conds.push(formData[key])
                }
            }
            if(conds.length==0)return false
            var result=identify(res.data,conds)
            $("#induction_conclusion").html("<p>"+result+"</p>")
            return false
        });
        },       
        error:function (data) {
        }     
      });
}

/**
 * 反向推理
 */
function backward_induction(){

    $.ajax({
        url:'/show-rules',   
        method:'get',
        async:false,   
        dataType:'JSON',    
        success:function(res){ 
          
          layui.form.on('submit(btnSubmit2)', function (data) {
            $("#induction_detail").html("<p>开始推理过程。。。。</p")
            //表单数据formData
            var formData = data.field;
            var conds=[]
            for (const key in formData) {
                if(key.indexOf('target_select')!=-1){
                    conds.push(formData[key])
                }
            }
            if(conds.length==0)return false
            var result=backward_check(res.data,conds)
            $("#induction_conclusion").html("<p>"+result+"</p>")
            return false
        });
        },       
        error:function (data) {
        }     
      });
}

function get_avali_rule(rules_data,target){

    var avali_rules=[]
    for (let i = 0; i < rules_data.length; i++) {
        const item = rules_data[i];
        if(item['ft_id']==target){
            avali_rules.push(item)
        }
    }

    return avali_rules;
}

function backward_check(rules_data,target){
    target=target[0]
    var avali_rules=get_avali_rule(rules_data,target)

    for (let i = 0; i < avali_rules.length; i++) {
        const item = avali_rules[i];
        $("#induction_detail").append("<p>"+item['conds']+"--------->"+item['result']+"</p")
        const conditions=item['conditions']
        for (let j = 0; j < conditions.length; j++) {
            if(conditions[j]['cond_type']==2){
                backward_check(rules_data,conditions[j]['cond_id'])
                // var avali_types=get_avali_rule(rules_data,conditions[j]['cond_id'])
                // for (let k = 0; k < avali_types.length; k++) {
                //     const item_type = avali_types[k];
                //     $("#induction_detail").append("<p>"+item_type['conds']+"--------->"+item_type['result']+"</p")
                // }
            }
            
        }
    }


    return "成功"
}